<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table border="1" width="500px" height="500px" align="center">
        <tr><!--  第一行 -->
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr><!--  第二行 -->
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr><!--  第三行 -->
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr><!--  第四行 -->
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>

    <script type="text/javascript">
        window.onload = function() {
            var tds = document.getElementsByTagName('td');
            var colors = ['red', 'pink', 'green', 'blue', 'black', 'yellow', 'purple'];
            for (var i = 0; i < tds.length; i++) {
                tds[i].style.backgroundColor = colors[Math.floor(Math.random() * 7)];
            }
        };
    </script>
</body>
</html>